/*
 * @Author: wurangkun
 * @Date: 2024-05-22 16:57:34
 * @LastEditTime: 2024-05-22 18:33:38
 * @LastEditors: wurangkun
 * @FilePath: \flutter_example\lib\tabbar.dart
 * @Description: 
 */
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_example/home/home.dart';
import 'package:flutter_example/userInfo/userinfo.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class TabbarPage extends StatefulWidget {
  const TabbarPage({super.key});

  @override
  State<TabbarPage> createState() => _TabbarPageState();
}

class _TabbarPageState extends State<TabbarPage> {
  late int _currentIndex = 0;
  List<Map<String, Object>> pages = [
    {"page": const HomePage(), "title": "首页"},
    {"page": const UserInfoPage(), "title": "我的"}
  ];
  final PageController _pageController = PageController();

  @override
  void initState() {
    super.initState();
    // _pageController.addListener(() {
    //   _pageController.jumpToPage(_pageController.page!.toInt());
    // });
  }

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  Widget _buildTabbar() {
    var tabbar = <Widget>[];
    for (var i = 0; i < pages.length; i++) {
      tabbar.add(Expanded(
        child: GestureDetector(
          onTap: () {
            setState(() {
              _currentIndex = i;
            });
            _pageController.jumpToPage(i);
          },
          child: Container(
              color: Colors.white,
              alignment: Alignment.center,
              child: Text(pages[i]['title'] as String,
                  style: TextStyle(
                      color: _currentIndex == i
                          ? const Color.fromARGB(255, 27, 84, 207)
                          : const Color(0xff333333),
                      fontSize: 14,
                      decoration: TextDecoration.none))),
        ),
      ));
    }
    return Flex(direction: Axis.horizontal, children: tabbar);
  }

  getmediaQuery() {
    MediaQueryData mediaQuery = MediaQuery.of(context);
    double w = mediaQuery.size.width; //屏幕宽度
    double h = mediaQuery.size.height; //屏幕高度
    double scale = mediaQuery.devicePixelRatio; //每一个逻辑像素包含多少个原始像素
    double statusBar = mediaQuery.padding.top; //状态栏高度
    print(mediaQuery);
    print(w);
    print(h);
    print(scale);
    print(statusBar);
  }

  @override
  Widget build(BuildContext context) {
    return Flex(direction: Axis.vertical, children: [
      Expanded(
          flex: 1,
          child: Container(
              height: double.infinity,
              color: Colors.white,
              child: PageView.builder(
                physics: const NeverScrollableScrollPhysics(),
                controller: _pageController,
                itemCount: pages.length,
                itemBuilder: (context, index) => pages[index]['page'] as Widget,
              ))),
      SafeArea(
        top: false,
        bottom: true,
        child: Container(
          height: 100.h,
          color: Colors.white,
          child: _buildTabbar(),
        ),
      )
    ]);
  }
}
